<?php

class Accountdb extends Model 
{
	
	function Accountdb()
	{     
		parent::Model();
	}
	
	/*********************************************
	* Get user data
	*
	* @param string $data , from Auth or Account db fields
	* @return object
	*
	* $udata = $this->accountdb->getUserData('City', 'State');
	*/	
	function getUserData($data)
	{
		$uid = $this->session->userdata('uid');
		$this->db->select($data);
		$this->db->from('Auth');
		$this->db->join('Accounts', 'Auth.uid = Accounts.uid');
		$this->db->where('uid', $uid);
		$result = $this->db->get();
		return $result;
	}
	
	/*********************************************
	* Get alias and password
	*
	* @param varchar $alias
	* @param varchar $pass
	* @return object
	*/
	function Login($a, $p)
	{
		$where = array('Alias'=>$a, 'Pass'=>$p);
		$this->db->where($where);
		$this->db->from('Auth');
		$data = $this->db->get();
		unset($data->Pass);
      return $data; 
	}
	
	/*********************************************
	* Get cookie
	*
	*
	* @return object
	*/
	function getCookie()
	{
		return $data = $this->getUserData('cookie');
	}
	
	
	/*********************************************
	* Get eMail, checks if the email address is already in our db
	* 
	* @param varchar $email
	* @return array
	*/
	function Email($email)
	{
		return $data = $this->getUserData('eMail');
	}
	
	/*********************************************
	* Register/Add new user in to db
	*
	* @param varchar $alias
	* @param varchar $email
	* @param varchar $timezone
	* @param varchar $pass
	* @return array
	*/
	function Register($alias, $email, $timezone)
	{	
		$data = array(
			'Alias' => $alias ,
			'Pass' => $this->fun->randString(),
			'IP' => $this->fun->getip()
			);
		$this->db->insert('Auth', $data);
		
		$uid = $this->db->insert_id();
		$data = array(
			'uid' => $uid,
			'newemail' => $email ,
			'GMT' => $timezone,
			'IP' => $this->fun->getip(),
			'date_joined' => 'NOW()'
			);
		$this->db->insert('Account', $data);
		
      return $data;
	}
	
	
}
?>
